package com.spring.nio.core;

import com.spring.nio.common.BasicConstant;
import com.spring.nio.util.LocalTime;
import com.spring.nio.util.PropertiesUtil;
import lombok.extern.slf4j.Slf4j;
import java.time.LocalDateTime;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Auther: YSFY
 * @Date: 2020-11-14 16:32
 * @Pacage_name: com.spring.nio.core
 * @Project_Name: net-server
 * @Description: 基础启动配置服务
 *
 *      加载配置文件记录配置文件数据 --> 创建启动Selector
 */

@Slf4j
@SuppressWarnings("all")
public class BasicServer {
    public static String SERVER_IP = "127.0.0.1", FILE_PATH = BasicConstant.DATA_EMPTY, NETTY_SERVER_PROTOCOL_PORT = "";
    // 公共共享数据集合
    private static final Map<String, Object> map = new ConcurrentHashMap<>();
    public final static Lock chatLock = new ReentrantLock(true);
    public final static Lock fileLock = new ReentrantLock(true);

    /**
     * 启动基础配置服务
     */
    public static void startupBasicServer() {
        // 1、读取配置文件
        loadConfigProperties();

        // 2、设置枚举
        setEnumsType();
    }

    /**
     * 读取配置文件数据并设置进缓存
     * */
    private static void loadConfigProperties() {
        PropertiesUtil.initProperties();

        map.put(BasicConstant.CPU_CORE_COUNT, Runtime.getRuntime().availableProcessors());
        map.put(BasicConstant.OS_NAME, System.getProperty(BasicConstant.OS_NAME));
        Iterator iterator = PropertiesUtil.getInstance().stringPropertyNames().iterator();
        while (iterator.hasNext()) {
            String param = iterator.next().toString();

            if(!map.containsKey(param)) {
                map.put(param, PropertiesUtil.getValue(param));
            }
        }
        log.info("[" + LocalTime.formatDate(LocalDateTime.now()) + "] BasicServer | loadConfigProperties --> 读取配置文件[server.properties], result = success, Server print Config");

        System.out.println("/+------------------------------------------ Server Properties Begin ------------------------------------------+/");
        System.out.println();
        Map.Entry<String, Object> entry = null;
        if(!map.isEmpty()) {
            iterator = map.entrySet().iterator();
            while (iterator.hasNext()) {
                System.out.println("/+------------------------------------------ " + (entry = (Map.Entry<String, Object>) iterator.next()).getKey() + " = " + entry.getValue() + " ------------------------------------------+/");
            }
        }
        System.out.println("/+------------------------------------------ Server Properties End ------------------------------------------+/");
        System.out.println();
    }

    /**
     * 设置枚举
     */
    private static void setEnumsType() {
        // 设置文件枚举
        /*FileMessageFrame.FrameType[] fileFrameTypes = FileMessageFrame.FrameType.values();
        Map<String, FileMessageFrame.FrameType> fileFrameTypeMap = Maps.newHashMap();
        for(FileMessageFrame.FrameType frameType : fileFrameTypes) {
            fileFrameTypeMap.put(frameType.getBit(), frameType);
        }
        map.put(BasicConstant.FILE_MESSAGE_FRAME_TYPE, fileFrameTypeMap);*/
    }

    /**
     * 返回缓存Map对象
     * */
    public static Map<String, Object> getMap() {
        return map;
    }
}
